package com.docchatlink.controller;

import com.docchatlink.dto.ResponseMsg;
import com.docchatlink.iflytek.WebSocketServer;
import com.docchatlink.iflytek.client.IflytekDocClient;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;

/**
 * 星火知识库V1.0接口
 *
 * @author Mr.piggy
 * @since 2024/3/27
 */
@RestController
public class ChatController {

    @Resource
    private IflytekDocClient iflytekDocClient;

    @Resource
    private WebSocketServer webSocketServer;

    /**
     * 文件上传
     *
     * @param file
     * @return
     * @throws IOException
     */
    @PostMapping("/upload")
    public ResponseMsg fileUpload(@RequestParam(required = false, value = "file") MultipartFile file) throws IOException {
        return ResponseMsg.success(iflytekDocClient.fileUpload(file), "上传成功");
    }

    /**
     * 发起文档总结
     *
     * @param fileId
     * @return
     */
    @PostMapping("/startSummary")
    public ResponseMsg startSummary(@RequestParam(required = false, value = "fileId") String fileId) {
        return ResponseMsg.success(iflytekDocClient.startSummary(fileId), "查询成功");
    }

    /**
     * 获取文档总结/概要内容
     */
    @PostMapping("/fileSummary")
    public ResponseMsg fileSummary(@RequestParam(required = false, value = "fileId") String fileId) {
        return ResponseMsg.success(iflytekDocClient.fileSummary(fileId), "查询成功");
    }

    /**
     * 初始化会话
     *
     * @return
     */
    @RequestMapping("/chatInit")
    public ResponseMsg chatInit() {
        String sid = iflytekDocClient.initChat();
        return ResponseMsg.success(sid, "对话初始化成功");
    }

    @PostMapping("/chat")
    public ResponseMsg chat(@RequestParam(required = false, value = "fileId") String fileId,
                            @RequestParam(required = false, value = "question") String question) {
        iflytekDocClient.chat(fileId, question);
        return ResponseMsg.success(null, "查询成功");
    }

    /**
     * 一次返回所有结果
     *
     * @param fileId
     * @param question
     * @return
     */
    @PostMapping("/chatAn")
    public ResponseMsg chatAn(@RequestParam(required = false, value = "fileId") String fileId,
                              @RequestParam(required = false, value = "question") String question) {
        String answer = iflytekDocClient.chatAn(fileId, question);
        return ResponseMsg.success(answer, "查询成功");
    }

    /**
     * 验证链接是否有效
     *
     * @param sid
     * @return
     */
    @RequestMapping("/checkChatAlive/{sid}")
    public ResponseMsg checkChatAlive(@PathVariable("sid") String sid) {
        return ResponseMsg.success(webSocketServer.sidAlive(sid), "查询成功");
    }

}
